/**
 * 
 */
package com.yidu.qx.utils;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Random;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.Part;

/**
 * 类的描述 工具类
 * @author 张超
 * @data 2019年4月23日上午9:44:46
 */
public class Tools {

	private String modb;
	private String chab;

	public String getModb() {
		return modb;
	}

	public void setModb(String modb) {
		this.modb = modb;
	}

	public String getChab() {
		return chab;
	}

	public void setChab(String chab) {
		this.chab = chab;
	}

	/**
	 * 生成一个20位的随机数
	 * @return
	 */
	public static String getRandom() {
		String randomStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssm");
		
		Random rd = new Random();
		String str = "";
		for(int i = 0;i<4;i++) {
			int t = rd.nextInt(randomStr.length());
			char c = randomStr.charAt(t);
			str+=c;
		}
		Date date = new Date();
		str = sdf.format(date)+str;
		return str;
	}
	
	/**
	 * 生成纯数字的随机数
	 * @return
	 */
	public static String getRandomNumber() {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddHHmmssm");
		String str = "";
		int c = (int)Math.random()*100;
		str+=c;
		
		Date date = new Date();
		str = sdf.format(date) + str;
		return str;
	}
	
	/**
	 * 生成随机字符串
	 * @param size 生成的长度
	 * @return
	 */
	public static String getRandom(int size) {
		String randomStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
		Random rd = new Random();
		String str = "";
		for(int i=0;i<size;i++) {
			int t = rd.nextInt(randomStr.length());
			char c = randomStr.charAt(t);
			str+=c;
		}
		return str;
	}
	
	/**
	 * 上传图片
	 * @param request
	 * @param name
	 * @return
	 * @throws IOException
	 * @throws ServletException
	 */
	public static String wite(HttpServletRequest request,String name) throws IOException, ServletException {
		//得到照片
		Part part = request.getPart(name);
		
		String upload = request.getServletContext().getRealPath("/upload");
		//得到上传文件名
		String fileName = part.getSubmittedFileName();
		
		String hz = fileName.substring(fileName.lastIndexOf("."));
		
		fileName = Tools.getRandom()+hz;
		
		part.write(upload+"/"+fileName);
		return "upload/"+fileName;
	}
	
	/**
	 *  	获取当前的日期和时间
	 * @return
	 */
	public static String getDatetime() {
		Date date = new Date();
		SimpleDateFormat simple = new SimpleDateFormat("yyyy-MM-dd hh:mm:dd");
		return simple.format(date);
	}
}
